import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpEvent, HttpHandler, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class HttpTokenInterceptors implements HttpInterceptor {
    constructor() {}

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        const token = localStorage.getItem('token');
        if (token) {
            const authReq = req.clone({
                headers: req.headers.append('Authorization', 'Bearer ' + token).append('X-Requested-With', 'XMLHttpRequest'),
            });
            return next.handle(authReq);
        } else {
            return next.handle(req);
        }
    }
}
